package com.bootdo.appsystem.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.UseraccountService;
import com.bootdo.common.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.MessageDO;
import com.bootdo.appsystem.service.MessageService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/message")
public class MessageController extends BaseController {
	@Autowired
	private MessageService messageService;
	@Autowired
	private UseraccountService useraccountService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:message:message")
	String Message(){
	    return "appsystem/message/message";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:message:message")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MessageDO> messageList = messageService.list(query);
		int total = messageService.count(query);
		PageUtils pageUtils = new PageUtils(messageList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("appsystem:message:add")
	String add(){
	    return "appsystem/message/add";
	}

	@GetMapping("/edit/{msgid}")
	@RequiresPermissions("appsystem:message:edit")
	String edit(@PathVariable("msgid") Integer msgid,Model model){
		MessageDO message = messageService.get(msgid);
		model.addAttribute("message", message);
	    return "appsystem/message/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:message:add")
	public R save( MessageDO message,String users){
		message.setAddtime(new Date());
		message.setCustomername(this.getUsername());
		message.setIsread(0);
		try {
            if(users.equals("所有人")){
                List<UseraccountDO> list = useraccountService.list(null);
                for (UseraccountDO user : list) {
                    message.setUid(user.getUid());
                    messageService.save(message);
                }
            }else {
                users = users.replace("，",",");
                String[] uids = users.split(",");
                for (String uid : uids) {
                    message.setUid(Integer.parseInt(uid)-10000);
                    messageService.save(message);
                }
            }
        }catch (RuntimeException e) {
            return R.error();
        }
		return R.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:message:edit")
	public R update( MessageDO message){
		messageService.update(message);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:message:remove")
	public R remove( Integer msgid){
		if(messageService.remove(msgid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:message:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] msgids){
		messageService.batchRemove(msgids);
		return R.ok();
	}
	
}
